// pages/Home/Home.js
const app = getApp();
const db = wx.cloud.database();
var sev = require('../../utils/sev.js');
/**
 * @Author 陈浩东
 */
Page({

  /**
   * 页面的初始数据
   */
  Getid(e) {
    this.setData({
      xh: e.detail.value
    })
  },
  Getpwd(e) {
    this.setData({
      pwd: e.detail.value
    })
  },
  data: {
    isStu: false,
    loadidngHidden: true,
    img: '',
    xh: '',
    pwd: '',
    elements: [{
        title: '课表',
        name: 'Class',
        color: 'white',
        icon: 'sort'
      },
      {
        title: '空闲教室',
        name: 'Freeroom',
        color: 'white',
        icon: 'usefull'
      },
      {
        title: '成绩',
        name: 'grade',
        color: 'blue',
        icon: 'text'
      },
      {
        title: '获奖记录',
        name: 'Reward',
        color: 'blue',
        icon: 'medal'
      },
     {
        title: '图书馆',
        name: 'Library',
        color: 'white',
        icon: 'read'
      },
      {
        title:'功能介绍',
        name:'Introduce',
        color:'white',
        icon:'info'
      },
      
    ]
  },
  tologin() {
    var that = this;
    this.setData({
      loadidngHidden: !this.data.loadidngHidden
    }) //请求加载动画
    var url = sev.eduoslg(this.data.xh, this.data.pwd);
    wx.cloud.callFunction({
      name: 'sync',
      data: {
        URL: url
      },
      success(res) {
        if (res.result.flag == '1') {
          console.log('登录成功') //设置缓存和app.js
          
          //console.log(res.result.token);
          app.eduOS.token = res.result.token;
          app.eduOS.xh = that.data.xh;
          //import 在这个地方开始查询时间信息
          wx.setStorage({
            key: 'educookie',
            data: {
              xh: that.data.xh,
              pwd: that.data.pwd
            }
          })
          wx.setStorage({
            key: 'token',
            data: res.result.token,
            success(res) {
              console.log('缓存设置成功'), that.setData({
                loadidngHidden: !that.data.loadidngHidden,
                isStu: !that.data.isStu
              })
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    this.setData({
      img: app.globalData.imgList.bgimg
    }) //设置首页背景图片
    wx.getStorage({
      key: 'educookie',
      success: function(res) {
        that.setData({xh:res.data.xh,pwd:res.data.pwd});
        that.tologin();
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  onShareAppMessage(){
    
  }
})